package spade;

import java.util.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.filter.*;
//import org.jivesoftware.smackx.*;

import spade.*;

// This is a listener for IQ packets
class IQListener implements PacketListener {

    public void processPacket(Packet packet) {
    	System.out.println();
        System.out.println("IQ: " + packet.toXML());
    };
};

// This is a listener for MESSAGE packets
class MessageListener implements PacketListener {
    public void processPacket(Packet packet) {
    	if(packet.getClass()==Message.class)
    		System.out.println("MESSAGE: " + ((Message)packet).getBody());
    	else
    		System.out.println("MESSAGE: " + packet.toXML());
    };
};

// This is a listener for PRESENCE packets
class PresenceListener implements PacketListener {
    public void processPacket(Packet packet) {
        System.out.println("PRESENCE: " + packet.toXML());
    };
};


class MyAgent extends Agent {

	// Why the fuck do I have to remake the constructor? I love this fucking language - Gus
	public MyAgent(String jidname, String password, Integer portnumber) {
		super(jidname, password, portnumber);
	};

	public void _setup() {
		System.out.println("HELLO FROM _setup");
	};
};

class a {

    String cosa = "COSA";

    // New main with an agent
    public static void main(String[] args) {
    	XMPPConnection.DEBUG_ENABLED = true;
    	MyAgent ag = new MyAgent("javamierda@127.0.0.1", "secret", 5222);
    	ag.start();
    }


	public static void oldmain(String[] args) {
		boolean registered = true;
		XMPPConnection.DEBUG_ENABLED = true;

		// The Asyncronous Listener
		/*PacketListener myListener = new PacketListener() {
		        // This is a fucking anonymous class, like a lambda
                public void processPacket(Packet packet) {
                    // Do something with the incoming packet here
                    // Print it in XML
                    System.out.println(packet.toXML());
                }
        };*/

        // This thing configures the connection
		ConnectionConfiguration config = new ConnectionConfiguration("127.0.0.1", 5222);
		//ConnectionConfiguration config = new ConnectionConfiguration("jabberes.org", 5222);
		config.setCompressionEnabled(false);
		config.setSASLAuthenticationEnabled(true);

		System.out.println("HOLA");

		// This is the connection object
		XMPPConnection conn1 = new XMPPConnection(config);

		try {
		    // Try to connect
			conn1.connect();

    		//conn1.addPacketListener(myListener, null);

    		// Add the listeners with their filters
    		// Create 3 listeners (IQ, MESSAGE and PRESENCE)
    		IQListener iql = new IQListener();
    		MessageListener ml = new MessageListener();
    		PresenceListener pl = new PresenceListener();

    		// Create 3 filters
        	PacketFilter iqfilter = new PacketTypeFilter(IQ.class);
        	PacketFilter mfilter = new PacketTypeFilter(Message.class);
        	PacketFilter pfilter = new PacketTypeFilter(Presence.class);

        	// Add each listener with each filter
    		conn1.addPacketListener(iql, iqfilter);
    		conn1.addPacketListener(ml, mfilter);
    		conn1.addPacketListener(pl, pfilter);

			System.out.println(conn1);

			// Try to make login
			conn1.login("agent@127.0.0.1", "secret", "spade");
			//conn1.login("juagargi@jabberes.org", "mecongratula", "spade");
		}
		catch (Exception e) {
		    // Something went wrong, possibly the login
			System.out.println("EXCEPTION");
			System.out.println(e);
			registered = false;
		};
		System.out.println("A conectarse");
		if (!registered) {
			System.err.println("Maaaal, no estamos registrados");
			//System.exit(-1);
		    // Not registered on the jabber server
			try {
			    // This is the dict of user properties
				HashMap<String, String> mapa = new HashMap<String,String>();

				// We add the name
				mapa.put("name","pruebaname");

				// Try to create the account
				conn1.getAccountManager().createAccount("agent", "secret", mapa);
				//conn1.getAccountManager().createAccount("juagargi", "mecongratula", mapa);

				// Disconnect and reconnect
				conn1.disconnect();
				conn1.connect();

				// Try to login again
				conn1.login("agent@127.0.0.1", "secret", "spade");
				//conn1.login("juagargi@jabberes.org", "mecongratula", "spade");
			}
			catch (Exception e) {
			    // This is really fucked up
				System.err.println("EXCEPTION");
				System.err.println(e);
			};
		};
	};

};

